/*
 * @authors :Bin Mei
 * @date    :2017-07-28
 * @description：票交汇APP-h5 ->授权认证
 */
import {mBridge,mFormat,mValidate,minHistory,mCookies} from 'min-bridge';

let Auth =(nextState, replace,extra,cb)=>{
	/*
	 * 检测用户进入的页面是否一定需要登录；
	 * @param {Object} extra
	 * @param {String} extra[title] 页面标题
	 * @param {String} extra[requireAuth] 当前进入的页面是否需要登录，默认 false 
	*/
	let requireAuth = extra.requireAuth||false;
	// console.log(nextState)

		__DEBUG__ && console.log("requireAuth文件，全部cookie =="+(document.cookie)+"==end");
	if(requireAuth){
		let sessionId = mFormat.getSessionId('sessionId');
		var backTo = window.location.href;
		if(!sessionId||sessionId==""){
			__DEBUG__ && console.log("------------backTo",backTo);
			if(mValidate.isFromPHJ()){
				 __DEBUG__ && console.log("backTo",backTo);
				setTimeout(()=>{
					minHistory.goBack();
				},200)
				mBridge.goLogin(backTo);
			}else{
				replace({pathname:"/app/login",query:{backTo:backTo}});
				cb&&cb();
			};
			// return false;
		}else{ 
			mBridge.doAction('setTitle', extra);
			cb&&cb();
		};

	}else{
		mBridge.doAction('setTitle', extra);
		 __DEBUG__ && console.log("sessionId =="+(mFormat.getSessionId('sessionId')));
		cb&&cb();
	}
	
}

module.exports = Auth;